//
//  SPComment.m
//  Sport
//
//  Created by Cass on 2018/2/2.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPComment.h"
#import "NSAttributedString+YYText.h"
@implementation SPComment
- (NSAttributedString *)attributedText{
    if (self.toUser && self.toUser.nickname.length > 0) {
        //有回复
        NSString *textString = [NSString stringWithFormat:@"%@回复%@: %@",self.fromUser.nickname,self.toUser.nickname,self.text];
        NSMutableAttributedString *mutableattributedString = [[NSMutableAttributedString alloc]initWithString:textString];
        mutableattributedString.yy_font = [UIFont systemFontOfSize:13.0f];
        mutableattributedString.yy_color = [UIColor blackColor];
        mutableattributedString.yy_lineSpacing = 5.0f;
        
        NSRange fromUserRange = NSMakeRange(0, self.fromUser.nickname.length);
        [mutableattributedString yy_setTextHighlightRange:fromUserRange color:[UIColor grayColor] backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            nil;
        }];
        
        NSRange toUserRange = [textString rangeOfString:[NSString stringWithFormat:@"%@:",self.toUser.nickname]];
        [mutableattributedString yy_setTextHighlightRange:toUserRange color:[UIColor grayColor] backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            nil;
        }];
        return mutableattributedString;
    }else{
        //没有回复
        NSString *textString = [NSString stringWithFormat:@"%@: %@",self.fromUser.nickname,self.text];
        NSMutableAttributedString *mutableattributedString = [[NSMutableAttributedString alloc]initWithString:textString];
        mutableattributedString.yy_font = [UIFont systemFontOfSize:13.0f];
        mutableattributedString.yy_color = [UIColor blackColor];
        mutableattributedString.yy_lineSpacing = 5.0f;
        
        NSRange fromUserRange = NSMakeRange(0, self.fromUser.nickname.length + 1);
        [mutableattributedString yy_setTextHighlightRange:fromUserRange color:[UIColor grayColor] backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            nil;
        }];
        return mutableattributedString;
    }
    return nil;
}
@end
